package com.wsc.wisdomchina.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.javaBean.wisdomParty.PartyContactListBean;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 党员通讯录适配器
 * Created by ${wangtao} on 2017/3/14.
 */

public class PartyContactAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<PartyContactListBean> messageBeen;

    public PartyContactAdapter(Context context, List<PartyContactListBean> messageBeen) {
        this.context = context;
        this.messageBeen = messageBeen;
    }
    /**
     *
     * 获取组的个数
     *
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupCount()
     */
    @Override
    public int getGroupCount()
    {
        return messageBeen.size();
    }

    /**
     *
     * 获取指定组中的子元素个数
     *
     * @param groupPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
     */
    @Override
    public int getChildrenCount(int groupPosition)
    {
        return messageBeen.get(groupPosition).getPm().size();
    }

    /**
     *
     * 获取指定组中的数据
     *
     * @param groupPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getGroup(int)
     */
    @Override
    public Object getGroup(int groupPosition)
    {
        return messageBeen.get(groupPosition);
    }

    /**
     *
     * 获取指定组中的指定子元素数据。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getChild(int, int)
     */
    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        return messageBeen.get(groupPosition).getPm().get(childPosition);
    }
    /**
     *
     * 获取指定组的ID，这个组ID必须是唯一的
     *
     * @param groupPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupId(int)
     */
    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }

    /**
     *
     * 获取指定组中的指定子元素ID
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#getChildId(int, int)
     */
    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    /**
     *
     * 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
     *
     * @return
     * @see android.widget.ExpandableListAdapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds()
    {
        return true;
    }

    /**
     *
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded 该组是展开状态还是伸缩状态
     * @param convertView 重用已有的视图对象
     * @param parent 返回的视图对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View,
     *      android.view.ViewGroup)
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        GroupHolder groupHolder = null;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.expandcontacelist_layout, null);
            groupHolder = new GroupHolder(convertView);
            convertView.setTag(groupHolder);
        }
        else
        {
            groupHolder = (GroupHolder)convertView.getTag();
        }

        if (!isExpanded)
        {
            groupHolder.img.setImageResource(R.mipmap.go2x);
        }
        else
        {
            groupHolder.img.setImageResource(R.mipmap.listx2x);
        }

        groupHolder.name.setText(messageBeen.get(groupPosition).getTypeName());
        groupHolder.count.setText(messageBeen.get(groupPosition).getPm().size()+"人");
        return convertView;
    }

    /**
     *
     * 获取一个视图对象，显示指定组中的指定子元素数据。
     *
     * @param groupPosition 组位置
     * @param childPosition 子元素位置
     * @param isLastChild 子元素是否处于组中的最后一个
     * @param convertView 重用已有的视图(View)对象
     * @param parent 返回的视图(View)对象始终依附于的视图组
     * @return
     * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View,
     *      android.view.ViewGroup)
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        ChildHolder itemHolder = null;
        if (convertView == null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.expandcontacechild_layout, null);
            itemHolder = new ChildHolder(convertView);
            convertView.setTag(itemHolder);
        }
        else
        {
            itemHolder = (ChildHolder)convertView.getTag();
        }
        if(childPosition==0)
        {
            itemHolder.view_line.setVisibility(View.VISIBLE);
        }else {
            itemHolder.view_line.setVisibility(View.GONE);
        }
        itemHolder.tv_name.setText(messageBeen.get(groupPosition).getPm().get(childPosition).getName());
        itemHolder.tv_zhiwu.setText(messageBeen.get(groupPosition).getPm().get(childPosition).getPartyDuties());
        Glide.with(context).load(messageBeen.get(groupPosition).getPm().get(childPosition).getHeadImg())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.own_pic_logout)
                .into((ImageView) itemHolder.iv_head);
        return convertView;
    }

    /**
     *
     * 是否选中指定位置上的子元素。
     *
     * @param groupPosition
     * @param childPosition
     * @return
     * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

    public class GroupHolder {

        private TextView name;
        private TextView count;
        private ImageView img;

        public GroupHolder(View view) {
            name = (TextView) view.findViewById(R.id.name);
            count=(TextView) view.findViewById(R.id.count);
            img=(ImageView) view.findViewById(R.id.img);
        }
    }

    public class ChildHolder {

        private TextView tv_name;
        private TextView tv_zhiwu;
        private CircleImageView iv_head;
        private View view_line;

        public ChildHolder(View view) {
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_zhiwu=(TextView) view.findViewById(R.id.tv_zhiwei);
            iv_head=(CircleImageView) view.findViewById(R.id.img);
            view_line=view.findViewById(R.id.view);
        }
    }

}
